//六Koa中间件
//构建一个 Koa 中间件，实现以下功能：
import Koa from 'koa';
import Router from 'koa-router'

let app = new Koa();
let router = new Router

let port = 7000;

//token
let VALID_TOKEN = 'valid-token-12';

//中间件
let Middwork = async (ctx, next) => {
    let token = ctx.headers['x-auth-token']

    if (token === VALID_TOKEN) {
        await next();
    } else {
        ctx.this.status = 401;
        ctx.body = {
            code: 401,
            message: 'what shit!'
        }
    }
}
// 应用中间件
app.use(authMiddleware);

// 定义路由
router.get('/', (ctx) => {
    ctx.body = 'Welcome to the protected route!';
});

// 注册路由
app.use(router.routes()).use(router.allowedMethods());

// 启动服务器
app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});